package cn.zucc.edu.madq.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.zucc.edu.madq.pojo.ClubActivities;
import cn.zucc.edu.madq.pojo.RegisterActivities;
import cn.zucc.edu.madq.pojo.User;
import cn.zucc.edu.madq.service.ClubActivitiesService;
import cn.zucc.edu.madq.service.RegisterActivitiesService;

@Controller
@RequestMapping("clubActivities")
public class ClubActivitiesController {

	@Autowired
	private ClubActivitiesService clubActivitiesService;
	@Autowired
	private RegisterActivitiesService registerActivitiesService;
	
	private static Logger logger = Logger.getLogger(ClubActivitiesController.class);
	
	@RequestMapping("getCountIdAndTitle")
	@ResponseBody
	public JSONPObject getCountIdAndTitle(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			List<Map<String, Object>> m = clubActivitiesService.getCountIdAndTitle(user.getUserId());
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, m);
		} catch (Exception e) {
			count = -1;
			//throw e;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}

	}
	
	@RequestMapping("getCountClubActivities")
	@ResponseBody
	public JSONPObject getCountClubActivities(
			@RequestParam("jsonpCallback") String jsonpCallback,
			@RequestParam("clubActivitiesId") int clubActivitiesId,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			List<Map<String, Object>> m = clubActivitiesService.getCountClubActivities(clubActivitiesId);
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, m);
		} catch (Exception e) {
			count = -1;
//			throw e;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}

	}
	
	@RequestMapping("delMyClubActivities")
	@ResponseBody
	public JSONPObject delMyClubActivities(
			@RequestParam("jsonpCallback") String jsonpCallback,
			@RequestParam("registerActivitiesId") int registerActivitiesId,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			count = clubActivitiesService.gdelMyClubActivitiesByRegisterActivitiesId(registerActivitiesId);
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		} catch (Exception e) {
			count = -1;
//			throw e;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}

	}
	
	@RequestMapping("getMyClubActivities")
	@ResponseBody
	public JSONPObject getMyClubActivities(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			List<Map<String, Object>> m = clubActivitiesService.getMyClubActivitiesByUserId(user.getUserId());
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, m);
		} catch (Exception e) {
			count = -1;
//			throw e;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}

	}
	
	@RequestMapping("joinClubActivities")
	@ResponseBody
	public JSONPObject joinClubActivities(
			@RequestParam("jsonpCallback") String jsonpCallback,
			@RequestParam("clubActivitiesId") int clubActivitiesId,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			RegisterActivities ra = new RegisterActivities();
			ra.setUserId(user.getUserId());
			ra.setClubActivitiesId(clubActivitiesId);
			count = registerActivitiesService.insert(ra); 
			return new JSONPObject(jsonpCallback, count);
		} catch (Exception e) {
			count = -1;
			//throw e;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}

	}
	
	@RequestMapping("getClubActivities")
	@ResponseBody
	public JSONPObject getClubActivities(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			List<Map<String, Object>> m = clubActivitiesService.getClubActivitiesByUserId(user.getUserId());
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, m);
		} catch (Exception e) {
			count = -1;
//			throw e;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}

	}
	
	@RequestMapping("addClubActivities")
	@ResponseBody
	public JSONPObject addClubActivities(
			@RequestParam("jsonpCallback") String jsonpCallback,
			@RequestParam("clubId") int clubId, 
			@RequestParam("clubActivitiesEndDate") long clubActivitiesEndDate, 
			@RequestParam("clubActivitiesBeginDate") long clubActivitiesBeginDate, 
			@RequestParam("clubActivitiesContent") String clubActivitiesContent, 
			@RequestParam("clubActivitiesTitle") String clubActivitiesTitle, 
			HttpServletRequest request,
			HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			ClubActivities ca = new ClubActivities();
			ca.setClubActivitiesBeginDate(new Date(clubActivitiesBeginDate));
			ca.setClubActivitiesEndDate(new Date(clubActivitiesEndDate));
			ca.setClubActivitiesContent(clubActivitiesContent);
			ca.setClubActivitiesManagerId(user.getUserId());
			ca.setClubActivitiesTitle(clubActivitiesTitle);
			ca.setClubId(clubId);
			count = clubActivitiesService.insert(ca);
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		} catch (Exception e) {
			count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			//throw e;
			return new JSONPObject(jsonpCallback, count);
		}
	}
}
